var breeze = {};
breeze.os = {};
breeze.os.isIOS = /iOS|iPhone|iPad|iPod/i.test(navigator.userAgent);
breeze.os.isAndroid = !breeze.os.isIOS;
breeze.callbacks = {}
breeze.callback = function (callbackName, response) {
   var callbackObj = breeze.callbacks[callbackName];
   console.log("execute callback "+callbackName);
   if (callbackObj !== undefined){
       // 获取key为callback的callback
       if(callbackObj.callback != undefined){
          console.log("execute callback response="+response);
           var ret = callbackObj.callback(response);
           if(ret === false){
               return
           }
           delete breeze.callbacks[callbackName];
       }
   }
}

breeze.callNativeFunction = function(commandname,parameters){
    console.log("breeze takeNativeAction")
    var request = {};
    request.name = commandname;
    request.param = parameters;
    if(window.breeze.os.isAndroid){
        console.log("android take native action" + JSON.stringify(request));
        // breezewebview是WebView中通过addJavascriptInterface添加的对象
        window.breezewebview.takeNativeAction(JSON.stringify(request));
    } else {
        // ios调用方式
        window.webkit.messageHandlers.breezewebview.postMessage(JSON.stringify(request))
    }
}
breeze.callNativeFunctionWithCallback = function(command, params, callback) {
    var callbackName = "nativetojs_callback_" +  (new Date()).getTime() + "_" + Math.floor(Math.random() * 10000);
    // 保存回调方法名称，待native回调时调用
    breeze.callbacks[callbackName] = {callback:callback};
    var request = {};
    request.name = command;
    request.param = params;
    // 增加回调方法的名称
    request.param.callbackName = callbackName;
    if(window.breeze.os.isAndroid){
        console.log("android take native action" + JSON.stringify(request));
        // breezewebview是WebView中通过addJavascriptInterface添加的对象
        window.breezewebview.takeNativeAction(JSON.stringify(request));
    } else {
        // ios调用方式
        window.webkit.messageHandlers.breezewebview.postMessage(JSON.stringify(request))
    }
}
window.breeze = breeze;
